################################################################################
# BAREFOOT NETWORKS CONFIDENTIAL & PROPRIETARY
#
# Copyright (c) 2018-2019 Barefoot Networks, Inc.

# All Rights Reserved.
#
# NOTICE: All information contained herein is, and remains the property of
# Barefoot Networks, Inc. and its suppliers, if any. The intellectual and
# technical concepts contained herein are proprietary to Barefoot Networks,
# Inc.
# and its suppliers and may be covered by U.S. and Foreign Patents, patents in
# process, and are protected by trade secret or copyright law.
# Dissemination of this information or reproduction of this material is
# strictly forbidden unless prior written permission is obtained from
# Barefoot Networks, Inc.
#
# No warranty, explicit or implicit is provided, unless granted under a
# written agreement with Barefoot Networks, Inc.
#
# Milad Sharif (msharif@barefootnetworks.com)
#
###############################################################################

import logging

import ptf.testutils as testutils
from bfruntime_base_tests import BfRuntimeTest
import bfrt_grpc.bfruntime_pb2 as bfruntime_pb2

logger = logging.getLogger('Test')
logger.addHandler(logging.StreamHandler())

class ExtactMatchTest(BfRuntimeTest):
    def runTest(self):
        ig_port = 1
        eg_port = 2
        dmac = '22:22:22:22:22:22'

        pkt = testutils.simple_tcp_packet(eth_dst=dmac)
        exp_pkt = pkt

        target = self.Target(device_id=0, pipe_id=0xffff)
        self.insert_table_entry(
            target,
            'SwitchIngress.forward',
            [self.KeyField('hdr.ethernet.dst_addr', self.mac_to_bytes(dmac))],
            'SwitchIngress.hit',
            [self.DataField('port', '\x00\x02')])

        logger.info("Sending packet on port %d", ig_port)
        testutils.send_packet(self, ig_port, str(pkt))

        logger.info("Expecting packet on port %d", eg_port)
        testutils.verify_packets(self, exp_pkt, [eg_port])

        self.delete_table_entry(
            target,
            'SwitchIngress.forward',
            [self.KeyField('hdr.ethernet.dst_addr', self.mac_to_bytes(dmac))])

        logger.info("Sending packet on port %d", ig_port)
        testutils.send_packet(self, ig_port, str(pkt))

        logger.info("Packet is expected to get dropped.")
        testutils.verify_no_other_packets(self)
